package com.lmabbe.core.service.impls;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmabbe.core.common.entitys.SysOrg;
import com.lmabbe.core.common.interfaces.SysOrgService;
import com.lmabbe.core.common.query.SysOrgQuery;
import com.lmabbe.core.service.dao.SysOrgMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lmabbe
 */
@DubboService(provider = "simple-dubbo")
public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> implements SysOrgService {

    @Override
    public List<SysOrg> getOrgListByParentId(String parentId) {
        LambdaQueryWrapper<SysOrg> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isEmpty(parentId)) {
            wrapper.isNull(SysOrg::getParentId);
        } else {
            wrapper.eq(SysOrg::getParentId, parentId);
        }
        wrapper.orderByAsc(SysOrg::getSortValue);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public IPage selectPage(SysOrgQuery query) {
        return baseMapper.selectPage(query.createPage(), query.createQuery());
    }

    @Override
    public List<SysOrg> getOrgAllParent(String orgId) {
        SysOrg sysOrg = baseMapper.selectById(orgId);
        return baseMapper.selectList(baseMapper.createQueryWrapper().in(SysOrg::getOrgId,sysOrg.getTreePath()));
    }
}
